package com.weimei.conversionTreasure.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gtdev5.geetolsdk.mylibrary.beans.LoginInfo;
import com.gtdev5.geetolsdk.mylibrary.beans.Vip;
import com.weimei.conversionTreasure.app.AppConfig;

import me.goldze.mvvmhabit.utils.SPUtils;


// 用户工具类
public class UserUtils {

    public static void setFreeNum(int free){
        SPUtils.getInstance().put("free",free);
    }

    public static int getFreeNum(){
        int num = SPUtils.getInstance().getInt("free");
        if (num > 0){
            return num;
        }else {
            return 0;
        }
    }

    public static void opreFree(){
        SPUtils.getInstance().put("free",getFreeNum()-1);
    }


    public static LoginInfo bean;
    public static Vip vipInfoBean;

    // 获取用户信息
    public static LoginInfo getUser() {
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            return bean;
        } else {
            return null;
        }
    }

    public static void isCheck(){
        String userStr = SPUtils.getInstance().getString("user");
        bean = new Gson().fromJson(userStr,new TypeToken<LoginInfo>(){}.getType());
    }

    public static void isCheckVip(){
        String userStr = SPUtils.getInstance().getString("vip");
        vipInfoBean = new Gson().fromJson(userStr,new TypeToken<Vip>(){}.getType());
    }


    // 判断是否登录
    public static boolean isLogin() {
        isCheck();
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            return true;
        } else {
            return false;
        }
    }

    public static void setVip(Vip vip) {
        if (ValidateUtils.isNotEmptyObjectOrString(vip)) {
            SPUtils.getInstance().put("vip",new Gson().toJson(vip));
            isCheckVip();
        }
    }

    public static boolean isVip(){
        isCheck();
        boolean isVip = false;
        if (ValidateUtils.isNotEmptyObjectOrString(vipInfoBean)) {
            isVip = vipInfoBean.isIsout();
        }
        return !isVip;
    }

    public static String getVipEndTime() {
        isCheck();
        String VipDay = "";
        if (ValidateUtils.isNotEmptyObjectOrString(vipInfoBean)) {
            VipDay = vipInfoBean.getTime()+"";
        }
        return ValidateUtils.isCheck(VipDay);
    }

    // 设置用户信息
    public static void setUser(LoginInfo userBean) {
        if (ValidateUtils.isNotEmptyObjectOrString(userBean)) {
            SPUtils.getInstance().put("user",new Gson().toJson(userBean));
            isCheck();
        }
    }

    public static String getUserId() {
        isCheck();
        String userId = "";
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            userId = bean.getUser_id()+"";
        }
        return ValidateUtils.isCheck(userId);
    }

    public static String getUserAccount() {
        isCheck();
        String userId = "";
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            if (ValidateUtils.isNotEmptyString(bean.getUser_id())){
                userId = bean.getUser_id()+"";
            }
        }
        return ValidateUtils.isCheck(userId);
    }

    public static String getUserPhone() {
        isCheck();
        String phone = "";
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            phone = bean.getTel();
        }
        return ValidateUtils.isCheck(phone);
    }


    public static String getUserName() {
        isCheck();
        String name = "";
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            if (ValidateUtils.isNotEmptyString(bean.getNickname())){
                name = bean.getNickname();
            }else {
                name = AppConfig.APP_NAME;
            }
        }
        return ValidateUtils.isCheck(name);
    }


    public static String getUserImg() {
        isCheck();
        String img = "";
        if (ValidateUtils.isNotEmptyObjectOrString(bean)) {
            img = bean.getHeadimg();
        }
        return ValidateUtils.isCheck(img);
    }

    // 清除用户信息
    public static void clear() {
        bean = null ;
        SPUtils.getInstance().remove("user");
    }

    public static boolean isShowAd(String code){
        if (code.equals("1")){
            return true;
        }else {
            return false;
        }
    }

    public static void getAdStatus(){

    }

}
